<template>
  <div class="app-container">

    <el-row>
      <el-col :span="16">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['scxx:checklist:add']"
        >新增</el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['scxx:checklist:edit']"
        >修改</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['scxx:checklist:remove']"
        >删除</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['scxx:checklist:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="8">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
                 style="display: flex;justify-content: center">
          <el-form-item prop="xmEnabled">
            <el-select v-model="queryParams.xmEnabled" placeholder="请选择是否启用" clearable>
              <el-option
                v-for="dict in dict.type.is_use"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="xmCode">
            <el-input
              v-model="queryParams.xmCode"
              placeholder="请输入项目名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item class="yu" style="display: flex">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>


    <el-table v-loading="loading" :data="checklistList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="检查项目编码" align="center" prop="jcCode" />
      <el-table-column label="项目名称" align="center" prop="xmCode" />
      <el-table-column label="项目属性" align="center" prop="xmTypecode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jcxm_property" :value="scope.row.xmTypecode"/>
        </template>
      </el-table-column>
      <el-table-column label="检验标准" align="center" prop="xmNice" />
      <el-table-column label="计量单位" align="center" prop="dwName" />
      <el-table-column label="备注" align="center" prop="xmRemarks" />
      <el-table-column label="是否启用" align="center" prop="xmEnabled">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_use" :value="scope.row.xmEnabled"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['scxx:checklist:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['scxx:checklist:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改检查项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="xmCode">
              <el-input v-model="form.xmCode" placeholder="请输入项目名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目属性" prop="xmTypecode">
              <el-select v-model="form.xmTypecode" placeholder="请选择项目属性编码">
                <el-option
                  v-for="dict in dict.type.jcxm_property"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-form-item label="检验标准" prop="xmNice">
            <el-input v-model="form.xmNice" placeholder="请输入检验标准" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="最小值" prop="xmMin">
              <el-input v-model="form.xmMin" placeholder="请输入项目最小值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最大值" prop="xmMax">
              <el-input v-model="form.xmMax" placeholder="请输入项目最大值" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="物料单位" prop="dwCode">
              <el-input v-model="form.dwCode" placeholder="请输入" readonly
                        @click.native="JldwDialogVisible=true;getJldwList()" suffix-icon="el-icon-more"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="xmEnabled">
              <el-select v-model="form.xmEnabled" placeholder="请选择是否启用">
                <el-option
                  v-for="dict in dict.type.is_use"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="备注" prop="xmRemarks">
            <el-input v-model="form.xmRemarks" placeholder="请输入备注" />
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--    选择物料单位弹出层-->
    <el-dialog
      title="选择物料单位"
      :visible.sync="JldwDialogVisible"
      width="30%">
      <el-container>
        <el-header>

          <el-row>
            <el-col :span="14">
              <el-input suffix-icon="el-icon-search" @keyup.enter.native="selectJldwWithName"
                        v-model="selectJldw.dwName"></el-input>
            </el-col>
            <el-col :span="10">
              <el-button plain style="margin-left: 10px" @click="selectJldwWithName()" type="primary">搜索</el-button>
              <el-button plain style="margin-left: 5px" @click="selectJldw.dwName='';getJldwList();" type="info">重置</el-button>
            </el-col>
          </el-row>
        </el-header>
        <el-main>

          <!-- 展示物料单位信息 -->
          <el-table
            :data="dwData"
            height="250"
            border
            style="width: 100%"
            @row-click="getdwData"
            :header-cell-style="{'text-align':'center'}"
          >
            <el-table-column
              prop="dwCode"
              label="单位编码"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="dwName"
              label="单位名称"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="dwEasy"
              label="单位简写"
              align="center"
            >
            </el-table-column>
          </el-table>
          <pagination
            v-show="jldwTotal>0"
            :total="jldwTotal"
            :page.sync="selectJldw.pageNum"
            :limit.sync="selectJldw.pageSize"
            @pagination="getJldwList"
          />
        </el-main>
      </el-container>
    </el-dialog>
  </div>
</template>

<script>
import { listChecklist, getChecklist, delChecklist, addChecklist, updateChecklist } from "@/api/scxx/checklist";
import { getJldw } from "@/api/scxx/wlMess";

export default {
  name: "Checklist",
  dicts: ['is_use', 'jcxm_property'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检查项目表格数据
      checklistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xmCode: null,
        xmTypecode: null,
        xmNice: null,
        xmMax: null,
        xmMin: null,
        dwCode: null,
        xmRemarks: null,
        xmEnabled: null,
        isDelete: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        xmCode: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        xmTypecode: [
          { required: true, message: "项目属性编码不能为空", trigger: "change" }
        ],
        xmNice: [
          { required: true, message: "检验标准不能为空", trigger: "blur" }
        ],
        xmMax: [
          { required: true, message: "项目最大值不能为空", trigger: "blur" }
        ],
        xmMin: [
          { required: true, message: "项目最小值不能为空", trigger: "blur" }
        ],
        dwCode: [
          { required: true, message: "计量单位编码不能为空", trigger: "blur" }
        ],
        xmRemarks: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        xmEnabled: [
          { required: true, message: "是否启用不能为空", trigger: "change" }
        ],
        isDelete: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ]
      },
      // 选择计量单位的弹出层
      JldwDialogVisible: false,
      // 计量单位信息
      dwData:[],
      // 计量单位搜索值
      selectJldw: {
        pageNum: 1,
        pageSize: 10,
        dwName: "",
      },
      // 计量单位总条数
      jldwTotal:0
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 根据计量单位名模糊查询
    selectJldwWithName(){
      if (this.selectJldw.dwName.trim() != "" ){
        this.getJldwList();
      }
    },
    // 获取计量单位选择的数据
    getdwData(row, column, event){
      this.form.dwCode = row.dwCode
      this.JldwDialogVisible = false
    },
    // 根据条件查询计量单位
    getJldwList(){
      getJldw(this.selectJldw).then(response => {
        this.dwData = response.rows;
        this.jldwTotal = response.total;
      });
    },
    /** 查询检查项目列表 */
    getList() {
      this.loading = true;
      listChecklist(this.queryParams).then(response => {
        this.checklistList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        jcCode: null,
        xmCode: null,
        xmTypecode: null,
        xmNice: null,
        xmMax: null,
        xmMin: null,
        dwCode: null,
        xmRemarks: null,
        xmEnabled: null,
        isDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.jcCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检查项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const jcCode = row.jcCode || this.ids
      getChecklist(jcCode).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检查项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.xmMin >= this.form.xmMax){
        this.$message('最小值不可大于或等于最大值');
        return false
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.jcCode != null) {
            updateChecklist(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChecklist(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jcCodes = row.jcCode || this.ids;
      this.$modal.confirm('是否确认删除检查项目编号为"' + jcCodes + '"的数据项？').then(function() {
        return delChecklist(jcCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('scxx/checklist/export', {
        ...this.queryParams
      }, `checklist_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scope lang="scss">
.yu {
  .el-form-item__content {
    display: flex;
  }
}
</style>
